from django.db import models

# Create your models here.

'''
创表思路:
    先写普通字段
    之后再写外键字段
'''
from django.contrib.auth.models import AbstractUser


class UserInfo(AbstractUser):
    # 手机号字段
    phone = models.BooleanField(verbose_name='手机号', null=True, blank=True)
    """
    在早期版本中,
        nuLL=True   数据库该字段可以为空
        bLank=True  admin后台管理该字段可以为空
    """
    # 头像字段
    avatar = models.FileField(upload_to='avatar/', default='avatar/default.png', verbose_name='用户头像')
    '''给avatar字段传文件对象该文件会自动存储到avatar文件下然后avatar字段只保存文件路径avatar/default.png'''
    # 创建时间
    creat_time = models.DateField(auto_now_add=True)

    # 外键字段
    blog = models.OneToOneField(to='Blog', null=True, on_delete=models.CASCADE)

    class Meta:
        verbose_name_plural = '用户表'   # 修改admin后台管理默认的表名
        # verbose_name = '用户表'  # 默认是verbose_name加s

    def __str__(self):
        return self.username


class Blog(models.Model):
    site_name = models.CharField(verbose_name='站点标题', max_length=32)
    sist_title = models.CharField(verbose_name='站点标题', max_length=32)
    # 简单模拟带你认识样式内部原理的操作
    sist_theme = models.CharField(verbose_name='站点样式', max_length=64)  # 存css/js的文件路径

    def __str__(self):
        return self.site_name


class Category(models.Model):
    name = models.CharField(verbose_name='文章分类', max_length=32)

    # 外键字段
    blog = models.ForeignKey(to='Blog', null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class Tag(models.Model):
    name = models.CharField(verbose_name='文章标签', max_length=32)

    # 外键字段
    blog = models.ForeignKey(to='Blog', null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class Article(models.Model):
    title = models.CharField(verbose_name='文章标题', max_length=64)
    desc = models.CharField(verbose_name='文章简介', max_length=255)
    # 文章内容有很多-般情况下都是使用TextField
    content = models.TextField(verbose_name='文章内容')
    create_time = models.DateField(auto_now_add=True)

    # 数据库字段设计优化
    up_num = models.BigIntegerField(verbose_name='点赞数', default=0)
    down_num = models.BigIntegerField(verbose_name='点踩数', default=0)
    comment_num = models.BigIntegerField(verbose_name='评论数', default=0)

    # 外键字段(3个)
    blog = models.ForeignKey(to='Blog', null=True, on_delete=models.CASCADE)
    category = models.ForeignKey(to='Category', null=True, on_delete=models.CASCADE)
    # 文章跟标签是多对多关系, 三种多对多创建方式, 我们选择自己创建第三张表
    tags = models.ManyToManyField(to='Tag',
                                  through='Article2Tag',
                                  through_fields=('article', 'tag')
                                  )

    def __str__(self):
        return self.title

class Article2Tag(models.Model):
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)
    tag = models.ForeignKey(to='Tag', on_delete=models.CASCADE)


class UpAndDown(models.Model):
    user = models.ForeignKey(to='UserInfo', on_delete=models.CASCADE)
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)

    # 记录点赞还是点踩
    is_up = models.BooleanField()  # 传布尔值, 存0/1


class Comment(models.Model):
    user = models.ForeignKey(to='UserInfo', on_delete=models.CASCADE)
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)
    content = models.CharField(verbose_name='评论内容', max_length=255)
    comment_time = models.DateTimeField(verbose_name='评论时间', auto_now_add=True)

    # 自关联
    parent = models.ForeignKey(to='self', null=True, on_delete=models.CASCADE)  # 有些评论就是根评论 null=True
    # parent = models.ForeignKey(to='Comment', null=True)    # 有些评论就是根评论 null=True

"""
在django2.0后，定义外键和一对一关系的时候需要加on_delete选项，此参数为了避免两个表里的数据不一致问题，不然会报错：
TypeError: __init__() missing 1 required positional argument: 'on_delete
"""